package com.itheima.day06.sort_bubble;

import java.util.Arrays;

//冒泡排序
//核心思想：相邻的元素两两比较，将最大的元素放在最后
//特点：比较轮数为数组长度-1
//     每轮比较的次数递减
public class TestMaopao {
    public static void main(String[] args) {
        //1.创建一个数组
        int[] arr = {10, 5, 6, 8, 9, 2, 1, 3, 4, 7};
        System.out.print("排序前的数组："+ Arrays.toString( arr));
        System.out.println("");
        int [] arr1 = {1,23,45,55,12,32,67,32,567,432};
        System.out.print("排序前的数组2："+ Arrays.toString( arr1));
        System.out.println("");


        compare(arr);
        System.out.println("");
        compare(arr1);
    }
    //2.开始排序
    private static void compare(int[] arr) {
        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = 0; j < arr.length - 1 - i; j++) {
                if (arr[j] < arr[j + 1]) {
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
    }
}
